<?php
namespace Cust\Controller;
use Cust\Controller\CustController;

/**
 * 用户管理模块
 */
class CustAdminController extends CustController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_permission = new \Common\Model\PermissionModel();
		$this->_cust_permission = new \Common\Model\CustPermissionModel();
	}
	
	//权限列表
	public function permissionListAction(){
		$ret = $this->_permission->getPermissionList();
		$this->data = $ret;
		$this->outPut();
	}
	
	
	//添加管理员用户
	public function addAdminCustAction(){
		$email = I('request.email');
		$password = I('request.password');
		$nickname = I('request.nickname');
		$realname = I('request.realname');
		$this->_addAdminCust($email,$password,$nickname,$realname);
	}
	
	private function _addAdminCust($email,$password,$nickname,$realname){
		if(empty($email) || empty($password)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		 
		 
		$ret = $this->_checkPermission($this->_custInfo['custid'] , 8);
    	if(!$ret){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();	
    	}
		 
		$time = time();
		if(!empty($email)){
			$sign = $this->_getSign($email, $time);
			$this->_checkUserExist($email,$time,$sign,2);
		}
		 
		$data = array();
		$data['email'] = $email;
		$data['password'] = \tools::encodePwd($password);
		$data['nickname'] = $nickname;
		$data['realname'] = $realname;
		$data['idenum'] = $this->_getIdeNum();
		$data['custtype'] = 2;
		$data['status'] = C('DEFAULT_CUST_STATUS');
		$custid = $this->_cust->addCust($data);
		 
		$data = array();
		$data['custid'] = $custid;
		$ret2 = $this->_cust_ext->saveCustExt($data);
		 
		$this->outPut();
	}
    
    
    //添加商户用户
    public function addMerCustAction(){
    	$email = I('request.email');
    	$password = I('request.password');
    	$nickname = I('request.nickname');
    	$contact = I('request.contact');
    	$contactmobile = I('request.contactmobile');
    	$realname = I('request.realname');
    	$address = I('request.address');
    	$qq = (int)I('request.qq');
    	$this->_addMerCust($email,$password,$nickname,$contact,$contactmobile,$realname,$address,$qq);
    }
    
    private function _addMerCust($email,$password,$nickname,$contact,$contactmobile,$realname,$address,$qq){
    	if(empty($email) || empty($password)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
		$ret = $this->_checkPermission($this->_custInfo['custid'] , 3);
    	if(!$ret){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	$time = time();
    	if(!empty($email)){
	    	$sign = $this->_getSign($email, $time);
	    	$this->_checkUserExist($email,$time,$sign,2);
    	}
    	
    	$data = array();
    	$data['email'] = $email;
    	$data['password'] = \tools::encodePwd($password);
    	$data['nickname'] = $nickname;
    	$data['realname'] = $realname;
    	$data['address'] = $address;
    	$data['idenum'] = $this->_getIdeNum();
    	$data['custtype'] = 4;
    	$data['status'] = C('DEFAULT_CUST_STATUS');
    	$custid = $this->_cust->addCust($data);
    	
    	$data = array();
    	$data['contact'] = $contact;
    	if(!empty($contactmobile)) $data['contactmobile'] = $contactmobile;
    	if(!empty($qq)) $data['qq'] = $qq;
    	$data['custid'] = $custid;
    	$ret2 = $this->_cust_ext->saveCustExt($data);
    	
    	$this->_setCustPermissions($custid,"1,4,5,6,9,10,12");//TODO注释掉
    	
    	$this->outPut();
    }
    
    
    //个人用户列表
    public function perCustListAction(){
    	$pageIndex = (int)I('request.pageIndex');
    	$pageSize = (int)I('request.pageSize');
    	$keywords = I('request.keywords');
    	$field = I('request.field');
    	$this->_perCustList($field,$keywords,$pageIndex,$pageSize);
    }
    
    private function _perCustList($field,$keywords,$pageIndex,$pageSize){
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	$data['searchKey'] = $keywords;
    	$data['field'] = $field;  //email mobile nickname
    	$data['custtype'] = 1;
    	$custList = $this->_cust->getCustListByFiled($data);
    	if(!empty($custList['data'])){
    		$tmp = array();
    		foreach($custList['data'] as $idx=>$cust){
    			$tmp[$idx]['custid'] = $cust['custid'];
    			$tmp[$idx]['mobile'] = $cust['mobile'];
    			$tmp[$idx]['custtype'] = $cust['custtype'];
    			$tmp[$idx]['nickname'] = $cust['nickname'];
    			$tmp[$idx]['realname'] = $cust['realname'];
    			$tmp[$idx]['age'] = $cust['age'];
    			$tmp[$idx]['sdtno'] = $cust['sdtno'];
    			$tmp[$idx]['sex'] = $cust['sex'];
    			$tmp[$idx]['university'] = $cust['university'];
    			$tmp[$idx]['school'] = $cust['school'];
    			$tmp[$idx]['majorclass'] = $cust['majorclass'];
    		}
    		$custList['data'] = $tmp;
    	}
    	
    	$this->data = $custList;
    	$this->outPut();
    }
    
    
    //商户用户列表
    public function merCustListAction(){
    	$pageIndex = (int)I('request.pageIndex');
    	$pageSize = (int)I('request.pageSize');
    	$keywords = I('request.keywords');
    	$field = I('request.field');
    	$this->_merCustList($field,$keywords,$pageIndex,$pageSize);
    }
    
    private function _merCustList($field,$keywords,$pageIndex,$pageSize){
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	$data['searchKey'] = $keywords;
    	$data['field'] = $field; //email mobile nickname
    	$data['custtype'] = 4;
    	$custList = $this->_cust->getCustListByFiled($data);
    	if(!empty($custList['data'])){
    		$tmp = array();
    		foreach($custList['data'] as $idx=>$cust){
    			$tmp[$idx]['custid'] = $cust['custid'];
    			$tmp[$idx]['contactmobile'] = $cust['contactmobile'];
    			$tmp[$idx]['custtype'] = $cust['custtype'];
    			$tmp[$idx]['nickname'] = $cust['nickname'];
    			$tmp[$idx]['realname'] = $cust['realname'];
    			$tmp[$idx]['email'] = $cust['email'];
    			$tmp[$idx]['address'] = $cust['address'];
    			$tmp[$idx]['qq'] = $cust['qq'];
    		}
    		$custList['data'] = $tmp;
    	}
    	
    	$this->data = $custList;
    	$this->outPut();
    }
    
    
    
    //厨师用户列表
    public function kitchenCustListAction(){
    	$pageIndex = (int)I('request.pageIndex');
    	$pageSize = (int)I('request.pageSize');
    	$keywords = I('request.keywords');
    	$field = I('request.field');
    	$this->_kitchenCustList($field,$keywords,$pageIndex,$pageSize);
    }
    
    private function _kitchenCustList($field,$keywords,$pageIndex,$pageSize){
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	$data['searchKey'] = $keywords;
    	$data['field'] = $field; //email mobile nickname
    	$data['iskitchener'] = 1;
    	$custList = $this->_cust->getCustListByFiled($data);
    	if(!empty($custList['data'])){
    		$tmp = array();
    		foreach($custList['data'] as $idx=>$cust){
    			$tmp[$idx]['custid'] = $cust['custid'];
    			$tmp[$idx]['contactmobile'] = $cust['contactmobile'];
    			$tmp[$idx]['custtype'] = $cust['custtype'];
    			$tmp[$idx]['nickname'] = $cust['nickname'];
    			$tmp[$idx]['realname'] = $cust['realname'];
    			$tmp[$idx]['email'] = $cust['email'];
    			$tmp[$idx]['address'] = $cust['address'];
    			$tmp[$idx]['qq'] = $cust['qq'];
    		}
    		$custList['data'] = $tmp;
    	}
    	 
    	$this->data = $custList;
    	$this->outPut();
    }
    
    
    //普通用户列表
    public function commonCustListAction(){
    	$pageIndex = (int)I('request.pageIndex');
    	$pageSize = (int)I('request.pageSize');
    	$keywords = I('request.keywords');
    	$field = I('request.field');
    	$this->_commonCustList($field,$keywords,$pageIndex,$pageSize);
    }
    
    private function _commonCustList($field,$keywords,$pageIndex,$pageSize){
    	if($pageIndex)$pageIndex--;
    	$data['pageIndex'] = $pageIndex;
    	$data['pageSize'] = $pageSize;
    	$data['searchKey'] = $keywords;
    	$data['field'] = $field; //email mobile nickname
    	$data['iskitchener'] = 0;
    	$custList = $this->_cust->getCustListByFiled($data);
    	if(!empty($custList['data'])){
    		$tmp = array();
    		foreach($custList['data'] as $idx=>$cust){
    			$tmp[$idx]['custid'] = $cust['custid'];
    			$tmp[$idx]['contactmobile'] = $cust['contactmobile'];
    			$tmp[$idx]['custtype'] = $cust['custtype'];
    			$tmp[$idx]['nickname'] = $cust['nickname'];
    			$tmp[$idx]['realname'] = $cust['realname'];
    			$tmp[$idx]['email'] = $cust['email'];
    			$tmp[$idx]['address'] = $cust['address'];
    			$tmp[$idx]['qq'] = $cust['qq'];
    		}
    		$custList['data'] = $tmp;
    	}
    
    	$this->data = $custList;
    	$this->outPut();
    }
    
    
    public function viewCustInfoAction(){
    	$custid = (int)I('request.custid');
    	$custInfo = $this->_getCustInfo($custid , 1);
    	$this->data = $custInfo;
    	$this->outPut();
    }
    
    public function delCustAction(){
    	$ret = $this->_checkPermission($this->_custInfo['custid'] , 8);
    	if(!$ret){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	$custid = (int)I('request.custid');
    	$this->_cust->deleteCustById($custid);
    	$this->outPut();
    }
    
    
    //更新用户信息
    public function updateCustAction(){
    	$custinfo = I('request.custinfo');
    	$this->_updateCust($custinfo);
    }
    
    private function _updateCust($custinfo){
    	if(empty($custinfo)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$custinfo = json_decode($custinfo , true);
    	if(empty($custinfo)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$data = array();
    	foreach($custinfo as $key=>$val){
    		if(in_array($key , $this->custFields)){
    			$data[$key] = $val;
    		}
    	}
    	$data['custid'] = $custinfo['custid'];
    	$ret1 = $this->_cust->updateCust($data);
    	 
    	$data = array();
    	foreach($custinfo as $key=>$val){
    		if(in_array($key , $this->custExtFields)){
    			if($key == "entranceyear") $val = date("Y-m-d" , strtotime($val));
    			$data[$key] = $val;
    		}
    	}
    	$data['custid'] = $custinfo['custid'];
    	$ret2 = $this->_cust_ext->saveCustExt($data);
    	 
    	if($ret1 && $ret2){
    		//if($field == "birthday")$custinfo['age'] = \tools::getAge($value);
    		$this->_setCustInfoCache($custinfo);
    		 
    		//$friendList = $this->_getFriendListCache($custinfo['custid']);
    		$friendList = $this->_getFriendList($custinfo['custid']);
    		if(!empty($friendList)){
    			foreach($friendList as &$friend){
    				$this->_delFriendListCache($friend['custid']);
    			}
    		}
    
    		$this->outPut();
    	}else{
    		$this->status = 1120;
    		$this->msg = L('STATUS_1120');
    		$this->outPut();
    	}
    }
    
    
    //设置管理后台权限
    public function setCustPermissionsAction(){
    	$custid = (int)I('request.custid');
    	$permissionids = I('request.permissionids');
    	$this->_setCustPermissions($custid,$permissionids);
    }
    
    private function _setCustPermissions($custid,$permissionids){
		if(empty($custid) || empty($permissionids)){
			$this->status = 1000;
    	 	$this->msg = L('STATUS_1000');
    	 	$this->outPut();
		}
		
		$ret = $this->_checkPermission($this->_custInfo['custid'] , 8);
		if(!$ret){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
    	 
		$permissionids = explode(",", $permissionids);
		if(!empty($permissionids)){
			$this->_cust_permission->delCustPermissions($custid);
			foreach($permissionids as $permissionid){
				$this->_cust_permission->setCustPermissions($custid, $permissionid);
			}
		}
    }
    
    
    
    
    private function _getChildNode($permissionList,$pid,$custPermissions){
    	$tmpArr = array();
    	if(!empty($permissionList)){
    		foreach($permissionList as $idx=>&$permission){
    			if($permission['pid'] == $pid && ($this->_custInfo['issuper'] || in_array($permission['permissionid'] , $custPermissions))){
    				$permissionid = (int)$permission['permissionid'];
    				$tmpArr[$permissionid] = $permission;
    			}
    		}
    	}
    	
    	$tmp = [];
    	if(!empty($tmpArr)){
    		foreach($tmpArr as $arr){
    			$tmp[] = $arr;
    		}
    	}
    	
    	return $tmp;
    }
    
    
    
    //获取用户的权限列表
    public function getPermissionsAction(){
    	$this->_getPermissions();
    }
    
    private function _getPermissions($custid){
    	$custid = $this->_custInfo['custid'];
    	
    	$custPermissions = $this->_cust_permission->getCustPermissions($custid);
    	
    	$permissionList = $this->_permission->getPermissionList();
    	$tmpArr1 = $this->_getChildNode($permissionList,"0",$custPermissions);
    	if(!empty($tmpArr1)){
    		foreach($tmpArr1 as &$tmp1){
    			$tmp1['child'] = $this->_getChildNode($permissionList,$tmp1['permissionid'],$custPermissions);//第一级子菜单
    			if(!empty($tmp1['child'])){
    				foreach($tmp1['child'] as &$tmp2){
    					$tmp2['child'] = $this->_getChildNode($permissionList,$tmp2['permissionid'],$custPermissions);//第二级子菜单
    					if(!empty($tmp2['child'])){
    						foreach($tmp2['child'] as &$tmp3){
    							$tmp3['child'] = $this->_getChildNode($permissionList,$tmp3['permissionid'],$custPermissions);//第二级子菜单
    							if(!empty($tmp3['child'])){
    								;//......
    							}else{
    								unset($tmp3['child']);
    							}
    						}
    					}else{
    						unset($tmp2['child']);
    					}
    				}
    			}else{
    				unset($tmp1['child']);
    			}
    		}
    	}
    	
    	
    	$this->data = $tmpArr1;
    	$this->outPut();
    }
    
    
    
    //获取用户的权限列表
    public function getCustPermissionsAction(){
    	$custid = (int)I('request.custid');
    	$this->_getCustPermissions($custid);
    }
    
    private function _getCustPermissions($custid){
    	if(empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$ret = $this->_checkPermission($this->_custInfo['custid'] , 8);
    	if(!$ret){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	 
    	 
    	$custPermissions = $this->_cust_permission->getCustPermissions($custid);
    	 
    	$this->data = $custPermissions;
    	$this->outPut();
    }
}
